/*
 * data_tran.c
 *
 *  Created on: Jun 23, 2025
 *      Author: lsy18
 */

#include "data_tran.h"

#include "math.h"

#include <stdio.h>
#include <string.h>


#include "usart.h"

#include "../Hardware/bmm150/bmm150.h"
#include "../Hardware/bmm150/bmm150_i2c.h"
#include "../Hardware/bmm150/bmm150_defs.h"

#include "../Communicate/Uart_slave/Uart_slave_REG/uart_slave_reg.h"

int16_t pos_init_data;

//char HexToAscii(uint8_t nHex) {
//if (nHex <= 9) {
//return (nHex + 0x30); // 数字0~9
//} else if (nHex >= 0x0A && nHex <= 0x0F) {
//return (nHex + 0x37); // 字母A~F
//} else {
//return 0xFF; // 错误值
//}
//}
//
//// 发送字符串
//void Usart_SendString(USART_TypeDef *pUSARTx, char *str) {
//unsigned int k = 0;
//do {
//Usart_SendByte(pUSARTx, *(str + k));
//k++;
//} while (*(str + k) != '\0');
//while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET) {}
//}

void bmm150_tran_jy62()
{
	uint16_t 	angle_data;
	int16_t 	angle_data_init;
	uint8_t angle_array[2] = {0};

//	uint8_t angle_array_test[2] = {0};

	angle_data_init = (int16_t)(atan2(magnet_data.y,magnet_data.x) * 180.0 / PI);
	angle_data_init = angle_data_init < 0 ? angle_data_init + 360 : angle_data_init;

	angle_data_init = angle_data_init - pos_init_data;
	angle_data_init = angle_data_init < 0 ? angle_data_init + 360 : angle_data_init;

	angle_data = (((double) angle_data_init) / 360 * 65535);

	angle_array[0] = angle_data & 0xff; // Convert angle to a 16-bit value (0-65535)
	angle_array[1] = angle_data >> 8;   // Convert angle to a 16-bit value (0-65535)

//	angle_array_test[1] = angle_data & 0xff; // Convert angle to a 16-bit value (0-65535)
//	angle_array_test[0] = angle_data >> 8;   // Convert angle to a 16-bit value (0-65535)

//
//	uart_slave_reg_write(angle_array_test,PITCH_L_REG, 2);
	//uart_slave_reg_write(angle_array, YAW_L_REG, 2); // Write the angle data to the YAW register
}





